package cn.tedu.zhyl.base.help;

import cn.tedu.zhyl.system.pojo.vo.SysDeptVo;
import cn.tedu.zhyl.system.pojo.vo.SysMenuVO;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

public class TreeHelper {

    public static List<SysMenuVO> buildTree(List<SysMenuVO> menus, int pid) {
        return menus
                .stream()
                .filter(p -> p.getPid() == pid)
                .map(p -> {
                    List<SysMenuVO> subChildren = buildTree(menus, p.getId());
                    if (!CollectionUtils.isEmpty(subChildren)) {
                        p.setChildren(subChildren);
                    } else {
                        p.setChildren(null);
                    }
                    return p;
                })
                .collect(Collectors.toList());
    }

    public static List<SysDeptVo> buildTree(List<SysDeptVo> depts, String pid) {
        return depts
                .stream()
                .filter(p -> p.getParentDeptNo().equals(pid))
                .map(p -> {
                    List<SysDeptVo> subChildren = buildTree(depts, p.getDeptNo());
                    if (!CollectionUtils.isEmpty(subChildren)) {
                       p.setChildren(subChildren);;
                    } else {
                        p.setChildren(null);
                    }
                    return p;
                })
                .collect(Collectors.toList());
    }
}
